<?php
// $Id$

/**
 * Controller_Withdraw 控制器
 */
class Controller_Withdraw extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
	}
	
	function actionNew(){
		
		if($this->_context->isPOST()){
			$user = $this->_app->currentUser();
			
			try {
				$_POST['user_id'] = $user['id'];
				$withdraw = new Withdraw($_POST);
				$withdraw->save();
				
				$member=Member::find('member_id = ?',$this->_context->post('member_id'))->getOne();
				
				$array['date']=date('Y-m-d');
				$array['time']=date('H:i:s');
				$array['username']=$member->username;
				$array['name']=$member->name;
				$array['bank_name'] = $member['bank_name'];
				$array['bank_account']=$member->bank_account;
				$array['telephone']=$member['phone'];
				$array['amount']=$this->_context->post('amount');
				$array['site']=$member->site->name;
				$array['administrator']=$user['username'];
				
				//send email notic administrator
				$message = Helper_Swift::messager();
				$mailer = Helper_Swift::mailer();
				
				$subject=Q::ini('appini/withdraw/subject');
				
				
				$message->setTo(array(Q::ini('appini/withdraw/to') => '财务部'));
				$message->setFrom(array(Q::ini('appini/smtp/email') => $user['username']));
				$body=$this->_mailBody();
				
				foreach($array as $key => $val){
					$search = '{$'.$key.'}';
					$subject=str_replace($search, $val, $subject);
					$body=str_replace($search, $val, $body);
				}
				$message->setSubject($subject);
				$message->setBody($body,'text/html' );
				
				$mailer->send($message);
					
				return json_encode(array('success'=>true));
			}
			catch (QDB_ActiveRecord_ValidateFailedException $e){
				return json_encode(array('errorMsg' => $e->getMessage()));
			}
			catch (QDB_Exception $e){
				return json_encode(array('errorMsg' => $e->getMessage()));
			}
		}
		
	}
	
	function _mailBody(){
	
		$body='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml">
				<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				</head>
				
				<body>
				<table border="1" cellpadding="2" cellspacing="1">
				 
				  <tr>
				    <td width="858" colspan="11" rowspan="2" align="center">（{$site}）出金确认报表</td>
				  </tr>
				  <tr> </tr>
				  <tr>
				    <td>时间</td>
				    <td>会员帐号</td>
				    <td>真实姓名</td>
				    <td>银行帐号</td>
				    <td>收款人开户行名称</td>
				    <td>出金金额</td>
				    <td>手续费</td>
				    <td>联系电话</td>
				    <td>转出银行</td>
				    <td>发件人</td>
				    <td>出金人</td>
				  </tr>
				  <tr>
				    <td>{$date} {$time}</td>
				    <td>{$username}</td>
				    <td>{$name}</td>
				    <td>{$bank_account}</td>
				    <td>{$bank_name}</td>
				    <td>{$amount}</td>
				    <td>　</td>
				    <td>{$telephone}</td>
				    <td>　</td>
				    <td>{$administrator}</td>
				    <td>　</td>
				  </tr>
				  <tr>
				    <td colspan="11" rowspan="2">　</td>
				  </tr>
				  <tr> </tr>
				</table>
				</body>
			</html>';
		
		return $body;
	}
}


